package cn.gov.msa.ws;

import java.util.List;

import org.springframework.context.ApplicationContext;

import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.Question;
import cn.gov.msa.util.GsonHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**   
* @Title: QuestionService.java 
* @Package cn.gov.msa.ws 
* @Description: 问卷调查(用一句话描述该文件做什么) 
* @author 冷云鹏  
* @date 2018年4月2日 上午10:08:56 
*/
public class QuestionService {
	
	public static String setData(ApplicationContext ctx, String params) throws Exception {
				
	    JSONObject jsonResult = new JSONObject();
	    cn.gov.msa.service.QuestionService questionService = (cn.gov.msa.service.QuestionService) ctx.getBean("QuestionService");  				        
		JSONObject jsonParams = JSONObject.fromObject(params);
		int pageNum = 1;
		int pageSize = 10;
		if(jsonParams.has("pageNum"))
		{
			pageNum = jsonParams.getInt("pageNum");
			jsonParams.remove("pageNum");
		}
		if(jsonParams.has("pageSize"))
		{
			pageSize = jsonParams.getInt("pageSize");
			jsonParams.remove("pageSize");
		}	
		Question question = GsonHelper.convertEntity(jsonParams.toString(), Question.class);
		List<Question> questionList=questionService.selectData(pageNum, pageSize, question);
	    JSONArray jaResult = JSONArray.fromObject(questionList);
	    //获取分页信息
        PageInfo page = new PageInfo(questionList);
        
        jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", jaResult);	
		
		return jsonResult.toString();
	}
	
	public static String setqData(ApplicationContext ctx, String params) throws Exception {
		String result = "";		
		
		JSONObject jsonResult = new JSONObject();
		cn.gov.msa.service.QuestionService questionService = (cn.gov.msa.service.QuestionService) ctx.getBean("QuestionService");  				        
		JSONObject jsonParams = JSONObject.fromObject(params);
		int pageNum = 1;
		int pageSize = 99;
		System.out.println(params);
		if(jsonParams.has("pageNum"))
		{
			pageNum = jsonParams.getInt("pageNum");
			jsonParams.remove("pageNum");
		}
		if(jsonParams.has("pageSize"))
		{
			pageSize = jsonParams.getInt("pageSize");
			jsonParams.remove("pageSize");
		}	
		Question question = GsonHelper.convertEntity(params.toString(), Question.class);
		
		List<Question> questionList =questionService.selectDataq(pageNum, pageSize, question);	
	    //获取分页信息
      
		JSONArray ja = JSONArray.fromObject(questionList);				

		jsonResult.put("rows", ja);				
		result = jsonResult.toString();
		return result;
	}
	
	public static String submit(ApplicationContext ctx, String params) throws Exception {
			
		JSONObject jsonResult = new JSONObject();
		cn.gov.msa.service.QuestionService questionService = (cn.gov.msa.service.QuestionService) ctx.getBean("QuestionService");  				        
		
		List<Question> questionList =GsonHelper.convertEntities(params.toString(), Question.class);
		for (Question question : questionList) {
			questionService.updateItemNum(question);
		}
				
		jsonResult.put("result", 1);				
		return jsonResult.toString();
	}
	
	
}
